package com.simpleframe.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultActionMessage implements ActionMessage {
	
	private Collection<String> errors;
    private Collection<String> messages;
    private Map<String, List<String>> fieldErrors;
    
	//@Override
	public void addError(String errorMessage) {
		if(errors == null){
			errors = new ArrayList<String>();
		}
		errors.add(errorMessage);
		
	}
	
	//@Override
	public void addEvent(String name, String event) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 一个字段有多个错误 的
	 */
	//@Override
	public void addFieldError(String fieldName, String errorMessage) {
		System.out.println("添加 "+fieldName);
		if(fieldErrors == null){
			fieldErrors = new HashMap<String, List<String>>();
		}
		//已存在的字段
		if(fieldErrors.containsKey(fieldName)){
			fieldErrors.get(fieldName).add(errorMessage);
		}else{//不存在的字段
			List<String> fieldMessageList = new ArrayList<String>();
			fieldMessageList.add(errorMessage);
			fieldErrors.put(fieldName, fieldMessageList);
		}
	}
	
	//@Override
	public void addMessage(String message) {
		if(messages==null){
			messages = new ArrayList<String>();
		}
		messages.add(message);
	}
	
	//@Override
	public boolean hasError() {
		if(errors!=null && errors.size() > 0){
			return true;
		}
		return false;
	}
	
	//@Override
	public boolean hasFieldError() {
		if(fieldErrors!=null && fieldErrors.size() > 0){
			return true;
		}
		return false;
	}
	
	//@Override
	public Map<String, List<String>> getFieldErrors() {
		return fieldErrors;
	}
	
	//@Override
	public Collection<String> getErrors() {
		return errors;
	}

	//@Override
	public Collection<String> getFieldErrorMessages() {
		List<String> fieldErrorMessages = new ArrayList<String>();
		if(hasFieldError()){
			for (String key : fieldErrors.keySet()) {
				fieldErrorMessages.addAll(fieldErrors.get(key));
			}
		}
		return fieldErrorMessages;
	}

	//@Override
	public Collection<String> getMessages() {
		return messages;
	}

	//@Override
	public boolean hasMessage() {
		if(messages!=null && messages.size() > 0){
			return true;
		}
		return false;
	}

	public boolean isAjaxRequest() {
		// TODO Auto-generated method stub
		return false;
	}
	
}
